<?php
// require 'vendor/autoload.php';
require '../../../make/vendor/autoload.php';		//引用make文件夹下的vendor

//本实例详见：https://www.qycn.com/xzx/article/16011.html
//创建ES类
//如果未设置密码
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
 
//如果es设置了密码
// $es = \Elasticsearch\ClientBuilder::create()->setHosts(['http://username:password@127.0.0.1:9200'])->build()
// var_dump($es);


// $data = $es->indices()->stats();		//获取 ES 的状态信息，包括index 列表

// 检查Index 是否存在
$params = [
	'index' => 'test3'
];
// $has = $es->indices()->exists($params);
// var_dump($has);


// 三、新建ES数据库
$params = [
	'index' => 'php_es', #index的名字不能是大写和下划线开头
	'body' => [
		'settings' => [
			'number_of_shards' => 2,
			'number_of_replicas' => 0
		]
	]
];
// $es->indices()->create($params);


//四、创建表（实测有问题）（可能是版本不兼容，测试环境是7.6.1版本）
$params = [
	'index' => 'autofelix_db',
	'type' => 'autofelix_table',
	'body' => [
		'mytype' => [
			'_source' => [
				'enabled' => true
			],
			'properties' => [
				'id' => [
					'type' => 'integer'
				],
				'first_name' => [
					'type' => 'text',
					'analyzer' => 'ik_max_word'
				],
				'last_name' => [
					'type' => 'text',
					'analyzer' => 'ik_max_word'
				],
				'age' => [
					'type' => 'integer'
				]
			]
		]
	]
];
// $es->indices()->putMapping($params);


//五、插入数据
$params = [
	'index' => 'php_es',
	'id' => 3, //可以手动指定id，也可以不指定随机生成
	'body' => [
		'id' => 3,
		'first_name' => '王五',
		'last_name' => '老王',
		'age' => 40
	]
];
// $es->index($params);


//更新
$params = [
	'index' => 'test3',
	'id' => 5,
	'body' => [
		'doc' => [
			'name' => '狂神55555',
			'age' => 5555,
			'desc' => '狂神无无无无'
		]
	]
];
// $es->update($params);


//删除（如果不指定id，则会删该索引下的所有数据）（如果指定id不存在，则报not_found错）
$params = [
	'index' => 'test3',
	'id' => 4
];
// $es->delete($params);


//六、 查询所有数据
$params = [
	'index' => 'php_es',
];
// $data = $es->search($params);


//七、查询单条数据
$params = [
	'index' => 'php_es',
	'id' => 3
];
// $data = $es->get($params);


//八、搜索
//非评分模式搜索
$params = [
	'index' => 'php_es',
	'body' => [
		'query' => [
			'constant_score' => [ //非评分模式执行
				'filter' => [ //过滤器，不会计算相关度，速度快
					'term' => [ //精确查找，不支持多个条件
						'first_name' => '飞'
					]
				]
			]
		]
	]
];
// 按条件查询
$params = [
	'index' => 'php_es',
	'body' => [
		'query' => [
			'match' => [
				'first_name' => '飞'
			]
		]
	]
];
//只查name，desc字段
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'match' => [
				'name' => '狂神'
			]
		],
		'_source' => ['name', 'desc']
	]
];
//age字段倒序排序
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'match' => [
				'name' => '狂神'
			]
		],
		'sort' => [
			[
				'age' => ['order' => 'desc']
			]
		]
	]
];
// 所有条件都要符合（相当于and）
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'bool' => [
				'must' => [
					[
						'match' => ['name' => '狂神']
					],
					[
						'match' => ['age' => 23]
					]
				]
			]
		],
		'_source' => ['name', 'age']
	]
];
//用空格分割，表示或（tags字段中匹配男，或技术）
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'match' => [
				'tags' => '男 技术'
				// 'name' => '李 N'
			]
		],
	]
];
// 过滤器，过滤查询（name字段匹配“狂神说”，并且age大于18，小于等于20）
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'bool' => [
				'must' => [
					[
						'match' => ['name' => '狂神说']
					]
				],
				'filter' => [
					'range' => [
						'age' => [
							'gt' => 18,
							'lte' => 20
						]
					]
				]
			]
		]
	]
];
//高亮
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'match' => [
				'name' => '狂神'
			]
		],
		'highlight' => [
			"pre_tags" => "<span class='key' style='color:red'>",
			"post_tags" => "</span>", 
			'fields' => [
				'name' => (object)[]
			]
		]
	]
];
// $data = $es->search($params);
//搜索并分页
$params = [
	'index' => 'test3',
	'body' => [
		'query' => [
			'match' => [
				'name' => '狂神'
			]
		],
		'sort' => [
			[
				'age' => ['order' => 'desc']
			]
		],
		'from' => 3,
		'size' => 2
	]
];
// $data = $es->search($params);

echo "<pre>";print_r($data);echo "</pre>";